//
//  YLYAddNewBillViewController.m
//  iAccounts
//
//  Created by 楊盧银Mac on 14-4-26.
//  Copyright (c) 2014年 com.yly16. All rights reserved.
//

#define Seconds_of_Minute 60
#define Minutes_of_Hour 60
#define Hours_of_Day 24
#import "YLYAddNewBillViewController.h"


@interface YLYAddNewBillViewController ()<UITextViewDelegate>

@end

@implementation YLYAddNewBillViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    
    return self;
}
-(void)viewWillAppear:(BOOL)animated{
    
    
//    self.Typelist = [[iBillTypeManager shareManager]TypeList];
    [self.iPickerView reloadAllComponents];
}
- (void)showBIllTypeAndAccount:(BOOL)isPayOut{
    //默认账单类别
    if (self.aBill != nil) {
        iBillType *type = [[DatabaseManager shareManager]showBillTypeByTypeID:_typeSub.typeID.integerValue];
        self.BillTypeLabel.text = [NSString stringWithFormat:@"%@-%@",type.typeName,_typeSub.typeSubName];
        self.billAccountLabel.text = [NSString stringWithFormat:@"%@",_accountSub.accounSubName];
        return;
    }
    self.Typelist = [[DatabaseManager shareManager]showAllBillType:(BOOL)self.BillPayOutSegment.selectedSegmentIndex];
    iBillType *type = self.Typelist[0];
    self.Detaillist = type.typeList;
    iBillTypeSub *tempSub = self.Detaillist[0];
    self.BillTypeLabel.text = [NSString stringWithFormat:@"%@-%@",type.typeName,tempSub.typeSubName];
    _typeSub = tempSub;
    //默认账单账户
    NSMutableArray *list = [[NSMutableArray alloc]init];
    list = [[DatabaseManager shareManager]showAllbillAccount];
    iBillAccount *account = list[0];
    list = account.accountList;
    iBillAccountSub *accountSub = list[0];
    self.billAccountLabel.text = [NSString stringWithFormat:@"%@",accountSub.accounSubName];
    _accountSub = accountSub;

    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    //>>>>>>>>>>>>>>>>>>定位>>>>>>>>>>>>>>>>>>>>>>>>>>
    //delegate
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    
    //The desired location accuracy.
    
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    //Specifies the minimum update distance in meters.
    
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    
    self.locationManager.purpose =
    
    @"To provide functionality based on user's current location.";
    
    [self.locationManager startUpdatingLocation];

    //--------------------------------------------
    [self showBIllTypeAndAccount:YES];
    self.Typelist = [[NSMutableArray alloc]init];
    self.Detaillist = [[NSMutableArray alloc]init];
    _currentNote = [[iBillNote alloc]init];
    _currentNote = [[DatabaseManager shareManager] currentNote];
    self.billImageVIew.layer.cornerRadius = self.billImageVIew.bounds.size.height/2;
    self.billImageVIew.layer.borderWidth = 3;
    self.billImageVIew.layer.borderColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"green.jpg"]].CGColor;
    self.billImageVIew.layer.masksToBounds = YES;
    
    self.KeyView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1];
    self.KeyView.layer.cornerRadius = 10;
    self.KeyView.layer.borderWidth = 1;
    self.KeyView.layer.masksToBounds = YES;
    self.KeyView.layer.borderColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"green.jpg"]].CGColor;
    self.KeyView.delegate = self;
    self.MoneyNumberStr = @"";
    
    for (UIView *view in self.CellBGViewArray) {
        view.layer.borderWidth = 1.5;
        view.layer.cornerRadius = 10;
        view.layer.borderColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"green.jpg"]].CGColor;
    }
    
    [self.BillPayOutSegment addTarget:self action:@selector(showChooseView:) forControlEvents:UIControlEventValueChanged];
    dtForMonth = [NSDate date];
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:dtForMonth];
    NSInteger month;
    NSInteger year;
    NSInteger day;
    
    month = [components month];
    year = [components year];
    day = [components day];
    
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSRange totaldaysForMonth = [gregorian rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:dtForMonth];//total days of particular month
    dtForMonth = [dtForMonth dateByAddingTimeInterval:Seconds_of_Minute*Minutes_of_Hour*Hours_of_Day*totaldaysForMonth.length];
    
    NSDateFormatter *dt = [[NSDateFormatter alloc] init];
    NSString *strMonthName = [[dt monthSymbols] objectAtIndex:month-1];//January,Febryary,March etc...
    
    self.title = [NSString stringWithFormat:@"%d-%@-%d",year,strMonthName,day];
    
    self.billDateLabel.text = [NSString stringWithFormat:@"%d-%d-%d （默认当天）",year,month,day];
    if (self.photoData != nil) {
        self.billImageVIew.image = [UIImage imageWithData:self.photoData];
    }
    if (self.aBill != nil) {
        [self quitCalculator];
        [self showBill:self.aBill];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}
-(void)showBill:(iBill*)aBill{
    self.BillPayOutSegment.selectedSegmentIndex = aBill.isPayOut.boolValue;
    _typeSub = [[DatabaseManager shareManager]showBillSubTypeByTypeID:aBill.billTypeSubID.integerValue];
    _accountSub = [[DatabaseManager shareManager]showAAccountByBillAccountSubID:aBill.billAccountSubID.integerValue];
    [self showBIllTypeAndAccount:self.BillPayOutSegment.selectedSegmentIndex];
    self.MoneyNumberTextView.text = [NSString stringWithFormat:@"💰%.2f",aBill.billMoney.floatValue];
    
    self.billDateLabel.text = [self stringFromDate:aBill.billTime];
    if (aBill.billImageData != nil) {
        self.billImageVIew.image = [UIImage imageWithData:aBill.billImageData];
    }
    self.iDataPicker.date = aBill.billTime;
    self.photoData = aBill.billImageData;
    self.billRemarkTextView.text = aBill.billRemark;
}

#pragma mark MoneyNumberLabel
-(IBAction)editMoneyNumberLabe{
    [self.MoneyNumberTextView resignFirstResponder];
    
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 1;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;
	animation.endProgress = 1;
	animation.removedOnCompletion = YES;
    animation.type = @"cube";//
    [self.KeyView.layer addAnimation:animation forKey:@"animation"];
    [self.KeyView setHidden:NO];
    [self.ChooseView setHidden:YES];
}



-(void)showCalculator:(NSString *)calculatorStr{
    [self.billRemarkTextView resignFirstResponder];
    self.MoneyNumberTextView.text = [NSString stringWithFormat:@"💰%@",calculatorStr];
}
-(void)quitCalculator{
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 0.5;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;
	animation.endProgress = 0.8;
	animation.removedOnCompletion = YES;
    animation.type = @"cube";//
    [self.KeyView.layer addAnimation:animation forKey:@"animation"];
    [self.KeyView setHidden:YES];
}

- (IBAction)showChooseView:(UIButton *)sender {
    [self.billRemarkTextView resignFirstResponder];
    _pickerType = pickerViewTypeBillType;
    [self.view setFrame:CGRectMake(0, 0, 320, 568)];
    self.Typelist = [[DatabaseManager shareManager]showAllBillType:(BOOL)self.BillPayOutSegment.selectedSegmentIndex];
    iBillType *temp = self.Typelist[0];
    self.Detaillist = temp.typeList;
    [self.iPickerView reloadAllComponents];
    [self.iDataPicker setHidden:YES];
    [self.iPickerView setHidden:NO];
    self.typeEditBarButton.enabled = YES;
    self.typeEditBarButton.title = @"编辑";
    
    
    [self showBIllTypeAndAccount:self.BillPayOutSegment.selectedSegmentIndex];
    
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 1;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;
	animation.endProgress = 1;
	animation.removedOnCompletion = YES;
    animation.type = @"rippleEffect";//
    [self.ChooseView.layer addAnimation:animation forKey:@"animation"];
    [self.ChooseView setHidden:NO];
    [self.KeyView setHidden:YES];
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basicAnimation.delegate = self;
    basicAnimation.fromValue = @(-M_PI);
    
    basicAnimation.timeOffset = 3.0f;
    basicAnimation.repeatCount = 3;
    basicAnimation.autoreverses = NO;
    [self.typeImageView.layer addAnimation:basicAnimation forKey:@"basic"];
}

- (IBAction)showBillAcountChoose:(UIButton *)sender {
    [self.billRemarkTextView resignFirstResponder];
    _pickerType = pickerViewTypeBillAccount;
    [self.view setFrame:CGRectMake(0, -100, 320, 668)];
    
    self.Typelist = [[DatabaseManager shareManager]showAllbillAccount];
    iBillAccount *acount = self.Typelist[0];
    self.Detaillist = acount.accountList;
    [self.iPickerView reloadAllComponents];
    [self.iDataPicker setHidden:YES];
    [self.iPickerView setHidden:NO];
    self.typeEditBarButton.enabled = YES;
    self.typeEditBarButton.title = @"编辑";
    
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 1;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;
	animation.endProgress = 1;
	animation.removedOnCompletion = YES;
    animation.type = @"rippleEffect";//
    [self.ChooseView.layer addAnimation:animation forKey:@"animation"];
    [self.ChooseView setHidden:NO];
    [self.KeyView setHidden:YES];
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basicAnimation.delegate = self;
    basicAnimation.fromValue = @(-M_PI);
    
    basicAnimation.timeOffset = 3.0f;
    basicAnimation.repeatCount = 3;
    basicAnimation.autoreverses = NO;
    [self.acountImageView.layer addAnimation:basicAnimation forKey:@"basic"];
}

- (IBAction)showDateChoose:(UIButton *)sender {
    [self.billRemarkTextView resignFirstResponder];
    _pickerType = pickerViewTypeBillDate;
    [self.iPickerView setHidden:YES];
    [self.iDataPicker setHidden:NO];
    self.typeEditBarButton.enabled = NO;
    self.typeEditBarButton.title = @"";
    
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 1;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;
	animation.endProgress = 1;
	animation.removedOnCompletion = YES;
    animation.type = @"rippleEffect";//
    [self.ChooseView.layer addAnimation:animation forKey:@"animation"];
    [self.ChooseView setHidden:NO];
    [self.KeyView setHidden:YES];
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basicAnimation.delegate = self;
    basicAnimation.fromValue = @(-M_PI);
    
    basicAnimation.timeOffset = 3.0f;
    basicAnimation.repeatCount = 3;
    basicAnimation.autoreverses = NO;
    [self.dateImageView.layer addAnimation:basicAnimation forKey:@"basic"];
}

- (IBAction)quitChooseView:(UIBarButtonItem *)sender {
    
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 1;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;
	animation.endProgress = 1;
	animation.removedOnCompletion = YES;
    animation.type = @"rippleEffect";//
    [self.ChooseView.layer addAnimation:animation forKey:@"animation"];
    [self.ChooseView setHidden:YES];
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basicAnimation.delegate = self;
    basicAnimation.fromValue = @(M_PI);
    
    basicAnimation.timeOffset = 3.0f;
    basicAnimation.repeatCount = 3;
    basicAnimation.autoreverses = NO;
    if (_pickerType == pickerViewTypeBillType) {
    
        [self.typeImageView.layer addAnimation:basicAnimation forKey:@"basic"];
    }
    if(_pickerType == pickerViewTypeBillAccount){
        [self.acountImageView.layer addAnimation:basicAnimation forKey:@"basic"];
    }
    if (_pickerType == pickerViewTypeBillDate) {
        [self.dateImageView.layer addAnimation:basicAnimation forKey:@"basic"];
        
    }
    [self.view setFrame:CGRectMake(0, 0, 320, 568)];
}

- (IBAction)editBillType:(UIBarButtonItem *)sender {
    [self performSegueWithIdentifier:@"EditBillType" sender:self];
}

- (IBAction)quitkeyboard:(id)sender {
    [self.billRemarkTextView resignFirstResponder];
}
//添加新账单
- (IBAction)addnewBill:(UIBarButtonItem *)sender {
    iBill *aBill = [[iBill alloc]init];
    NSString *str = [self.MoneyNumberTextView.text stringByReplacingOccurrencesOfString:@"💰" withString:@""];
    aBill.billMoney = [NSNumber numberWithDouble:str.doubleValue];
    aBill.isPayOut = [NSNumber numberWithBool:self.BillPayOutSegment.selectedSegmentIndex];
    aBill.billRemark = self.billRemarkTextView.text;
    aBill.billTime = [self.iDataPicker.date dateByAddingTimeInterval:-8*60*60];
    aBill.billlatitude = self.latitude;
    aBill.billlongitude = self.longitude;
    aBill.billTypeID = _typeSub.typeID;
    aBill.billTypeSubID = _typeSub.typeSubID;
    aBill.billImageData = self.photoData;
    aBill.billAccountID = _accountSub.accountID;
    aBill.billAccountSubID = _accountSub.accountSubID;
    aBill.billNoteID = _currentNote.noteID;
    UIAlertView *alert;
    NSString *alcStr;
    if (self.aBill == nil) {
    
        
        float number = [[self.MoneyNumberTextView.text stringByReplacingOccurrencesOfString:@"💰" withString:@""] floatValue];
        if (number == 0.00) {
            alcStr = @"请输入消费金额";
        }else{
            if([[DatabaseManager shareManager]addNewBill:aBill])
            {
                alcStr = @"添加成功";
            }else{
                alcStr = @"添加失败";
            }
        }
    }else{
        aBill.billID = self.aBill.billID;
        float number = [[self.MoneyNumberTextView.text stringByReplacingOccurrencesOfString:@"💰" withString:@""] floatValue];
        if (number == 0.00) {
            alcStr = @"请输入消费金额";
        }else{
            if([[DatabaseManager shareManager]ModifyBill:aBill])
            {
                alcStr = @"修改成功";
            }else{
                alcStr = @"修改失败";
            }
        }
    }
    
    
    alert = [[UIAlertView alloc] initWithTitle:@"=========" message:alcStr delegate:nil  cancelButtonTitle:@"ok" otherButtonTitles:nil];
    [alert show];
    [self performSelector:@selector(dimissAlert:) withObject:alert afterDelay:1.0];
}
- (void) dimissAlert: (UIAlertView *)alc {
    [alc dismissWithClickedButtonIndex:0 animated:YES];
}
- (IBAction)chooseBillImageFromPhoto:(UIButton *)sender {
    [self quitCalculator];
    [self quitChooseView:nil];
    UIActionSheet *actt = [[UIActionSheet alloc]initWithTitle:@"图片切换" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"选取照片" otherButtonTitles:@"保存照片", nil];
    [actt showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"选取照片"]) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
            UIImagePickerController *pickerView = [[UIImagePickerController alloc]init];
            
            pickerView.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
            pickerView.delegate = self;
            pickerView.allowsEditing= YES;
            [self presentViewController:pickerView animated:YES completion:nil];
        }
    }
    if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"保存照片"]) {
        UIImage *image = [UIImage imageWithData:self.photoData];
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image = info[UIImagePickerControllerEditedImage];
    
    
    self.photoData = UIImageJPEGRepresentation(image, 1.0f);
    self.billImageVIew.image = [UIImage imageWithData:self.photoData];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)chooseBillDate:(UIDatePicker *)sender {
    self.billDateLabel.text = [self stringFromDate:self.iDataPicker.date];
}
- (NSString *)stringFromDate:(NSDate *)date{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    
    
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    
    
    NSString *destDateString = [dateFormatter stringFromDate:date];
    
    
    return destDateString;
    
}
#pragma mark PickerViewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.Typelist.count;
    }else{
        return self.Detaillist.count;
    }
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    if (!view)
    {
        view = AH_AUTORELEASE([[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 30)]);
        
        UILabel *label = AH_AUTORELEASE([[UILabel alloc] initWithFrame:CGRectMake(35, 3, 245, 24)]);
        label.backgroundColor = [UIColor clearColor];
        [view addSubview:label];
        
        UIImageView *flagView = AH_AUTORELEASE([[UIImageView alloc] initWithFrame:CGRectMake(3, 3, 24, 24)]);
        flagView.contentMode = UIViewContentModeScaleToFill;
        [view addSubview:flagView];
    }
    if (_pickerType == pickerViewTypeBillType) {
        if (component == 0) {
            
            
            iBillType *type = self.Typelist[row];
            [(UILabel *)[view.subviews objectAtIndex:0] setText:type.typeName];
            UIImage *flag = [UIImage imageWithData:type.typeImageData];
            [(UIImageView *)[view.subviews objectAtIndex:1] setImage:flag];
        }
        else{

            
            iBillTypeSub *typeSub = self.Detaillist[row];
            [(UILabel *)[view.subviews objectAtIndex:0] setText:typeSub.typeSubName];
            UIImage *flag = [UIImage imageWithData:typeSub.typeSubImageData];
            [(UIImageView *)[view.subviews objectAtIndex:1] setImage:flag];
        }
    }
    if(_pickerType == pickerViewTypeBillAccount){
        if (component == 0) {

            
            iBillAccount *account = self.Typelist[row];
            [(UILabel *)[view.subviews objectAtIndex:0] setText:account.accountName];
            //*
            /*
            UIImage *flag = [UIImage imageWithData:type.typeImageData];
            [(UIImageView *)[view.subviews objectAtIndex:1] setImage:flag];
             */
        }
        else{

            
            iBillAccountSub *accountSub = self.Detaillist[row];
            [(UILabel *)[view.subviews objectAtIndex:0] setText:accountSub.accounSubName];
            //
            /*
            UIImage *flag = [UIImage imageWithData:typeSub.typeSubImageData];
            [(UIImageView *)[view.subviews objectAtIndex:1] setImage:flag];
             */
        }
    }
    
    return view;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (_pickerType == pickerViewTypeBillType) {
        iBillType *type;
        if (component == 0) {
            type = self.Typelist[row];
            typeStr = type.typeName;
            
            self.Detaillist = type.typeList;
            [self.iPickerView reloadComponent:1];
            if (self.Detaillist.count == 0) {
                return;
            }
            iBillTypeSub *typeSub = self.Detaillist[0];
            
            NSString *str = [NSString stringWithFormat:@"%@-%@",typeStr,typeSub.typeSubName];
            self.BillTypeLabel.text = str;
            _typeSub = typeSub;
            [self.iPickerView selectRow:0 inComponent:1 animated:YES];
            
        }else{
            iBillTypeSub *typeSub = self.Detaillist[row];
            if (self.BillPayOutSegment.selectedSegmentIndex == 0) {
                type = self.Typelist[typeSub.typeID.intValue-1];
            }
            else{
                type = self.Typelist[typeSub.typeID.intValue-[[[DatabaseManager shareManager]showAllBillType:NO] count]-1];
            }
            typeStr = type.typeName;
            NSString *str = [NSString stringWithFormat:@"%@-%@",typeStr,typeSub.typeSubName];
            self.BillTypeLabel.text = str;
            _typeSub = typeSub;
        }
        
    }
    else{
        iBillAccount *account;
        if (component == 0) {
            account = self.Typelist[row];
            typeStr = account.accountName;
         
            self.Detaillist = account.accountList;
            [self.iPickerView reloadComponent:1];
            iBillAccountSub *accountSub = self.Detaillist[0];
            
            _accountSub = accountSub;
            NSString *str = [NSString stringWithFormat:@"%@",accountSub.accounSubName];
            self.billAccountLabel.text = str;
            [self.iPickerView selectRow:0 inComponent:1 animated:YES];
        }else{
            
            iBillAccountSub *accountSub = self.Detaillist[row];
            account = self.Typelist[accountSub.accountID.intValue-1];
            typeStr = account.accountName;
            NSString *str = [NSString stringWithFormat:@"%@",accountSub.accounSubName];
            self.billAccountLabel.text = str;
            _accountSub = accountSub;
        }
    }
    
}
//TODO:>>>>> 传值 >>>>>>>
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    UIViewController *NewVC = [segue destinationViewController];
    
    [NewVC setValue:self.Typelist forKey:@"billTypeList"];
    
}
//TODO:LOcation
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    NSLog(@"didChangeAuthorizationStatus---%u",status);

}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"didChangeAuthorizationStatus----%@",error);

}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    NSLog(@"%@",locations);
    //得到newLocation
    CLLocation *loc = [locations objectAtIndex:0];
    self.latitude = [NSNumber numberWithFloat:loc.coordinate.latitude];
    self.longitude = [NSNumber numberWithFloat:loc.coordinate.longitude];
    
    [self.locationManager stopUpdatingLocation];
}

@end
